//
//  MyselfViewController.m
//  心水宝典
//
//  Created by zr on 2018/8/27.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "MyselfViewController.h"
#import "MyselfView.h"
#import "InfoViewController.h"
#import "SendSuggestViewController.h"
#import "SettingViewController.h"
#import "MyCollectionViewController.h"
#import "MyProblemViewController.h"
#import "MyTaiViewController.h"
#import "MyReplyViewController.h"
#import "MyMsgViewController.h"

@interface MyselfViewController ()

@property (nonatomic, strong)UIImageView * topImageView;
@property (nonatomic, strong)UIImageView * iconImageView;
@property (nonatomic, weak)UILabel * nameLabel;
@property (nonatomic, weak)UIView * whiteView;
@property (nonatomic, strong)UIButton * infoButton;
@property (nonatomic, strong)UIButton * troubleButton;
@property (nonatomic, weak)UIView * lineView;
@property (nonatomic, weak)UIView * verLineView;

@property (nonatomic, copy)NSURL * url;
@property (nonatomic, copy)NSString * sex;
@property (nonatomic, copy)NSString * name;
@property (nonatomic, copy)NSString * bri;

@end

@implementation MyselfViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self.navigationController setNavigationBarHidden:YES animated:animated];
    NSString * user_id = DEF_GET_OBJECT(@"userid");
    
//    NSLog(@"token===%@",token);
    NSString * str = [NSString stringWithFormat:@"/api/member/%@",user_id];
    
    [[UserInfoNetWorking sharedTools] GET:str parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary * data = responseObject[@"data"];
        
        NSURL * url = [NSURL URLWithString:data[@"head_link"]];
        [self.iconImageView sd_setImageWithURL:url placeholderImage:ImageNamed(@"23sdasas")];
        self.nameLabel.text = data[@"nickname"];
        
        self.url = url;
        self.name = data[@"nickname"];
        self.bri = data[@"birthday"];
        self.sex = data[@"sex"];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WHITE_COLOR;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ClickMyselfViewButton:) name:@"ClickMyselfViewButton" object:nil];
    
    [self setupUI];
}


- (void)setupUI{
    
    self.topImageView = [self setupImageViewWithImage:@"bj10"];
    [self.view addSubview:self.topImageView];
    
    UIImageView * iconImageView = [UIImageView new];
    
    iconImageView.image = ImageNamed(@"23sdasas");
    iconImageView.layer.cornerRadius = 40 * SCREENH6;
    iconImageView.layer.masksToBounds = YES;
    
    self.iconImageView = iconImageView;
    
    [self.view addSubview:self.iconImageView];
    
    
    UILabel * nameLabel = [UILabel new];
    
    nameLabel.textColor = WHITE_COLOR;
    nameLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    nameLabel.text = @"神奇测试";
    
    self.nameLabel = nameLabel;
    
    [self.view addSubview:self.nameLabel];
    
    
    UIView * whiteView = [UIView new];
    
    whiteView.backgroundColor = WHITE_COLOR;
    whiteView.layer.cornerRadius = 5 * SCREENH6;
    whiteView.layer.masksToBounds = YES;
    whiteView.userInteractionEnabled = YES;
    whiteView.layer.borderColor = [ColorWithRGB(0xC9C9C9) CGColor];
    whiteView.layer.borderWidth = 1.0f;
    
    self.whiteView = whiteView;
    
    [self.view addSubview:self.whiteView];
    
    self.infoButton = [self setupButtonWithImage:@"ziliao" andTitle:@"  个人资料"];
    [self.whiteView addSubview:self.infoButton];
    self.troubleButton = [self setupButtonWithImage:@"wenti" andTitle:@"  我的问题"];
    [self.whiteView addSubview:self.troubleButton];
    
    UIView * verLineView = [UIView new];
    
    verLineView.backgroundColor = ColorWithRGB(0x5F5F5F);
    
    self.verLineView = verLineView;
    
    [self.whiteView addSubview:self.verLineView];
    
    
    MyselfView * myselfView = [[MyselfView alloc] initWithFrame:CGRectMake(0, 295 * SCREENH6, SCREEN_WIDTH, 192 * SCREENH6)];;
    
    [self.view addSubview:myselfView];
}


- (void)viewDidLayoutSubviews{
    
    [self.topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.mas_equalTo(self.view);
        make.height.mas_equalTo(266 * SCREENH6);
    }];
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(80 * SCREENH6);
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.topImageView).offset(80 * SCREENH6);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(self.iconImageView.mas_bottom).offset(10 * SCREENH6);
    }];
    
    [self.whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).offset(219 * SCREENH6);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(310 * SCREENH6);
        make.height.mas_equalTo(55 * SCREENH6);
    }];
    
    [self.verLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(1 * SCREENH6);
        make.centerY.mas_equalTo(self.whiteView);
        make.height.mas_equalTo(25 * SCREENH6);
    }];
    
    [self.infoButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.bottom.mas_equalTo(self.whiteView);
        make.trailing.mas_equalTo(self.verLineView.mas_leading);
    }];
    
    [self.troubleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.top.bottom.mas_equalTo(self.whiteView);
        make.leading.mas_equalTo(self.verLineView.mas_trailing);
    }];
}


- (UIButton *)setupButtonWithImage:(NSString *)image andTitle:(NSString *)title{
    
    UIButton * button = [UIButton new];
    
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:BLACK_COLOR forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15 * SCREENH6];
    [button setImage:ImageNamed(image) forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clickWhiteButton:) forControlEvents:UIControlEventTouchUpInside];
    
    return button;
}


- (UIImageView *)setupImageViewWithImage:(NSString *)image{
    
    UIImageView * imageView = [UIImageView new];
    
    imageView.image = ImageNamed(image);
    
    return imageView;
}


- (void)clickWhiteButton:(UIButton *)button{
    
    HIDDEN_TAB;
    if (button == self.infoButton) {
        
        InfoViewController * infoVC = [InfoViewController new];
        
        infoVC.url = self.url;
        infoVC.name = self.name;
        infoVC.birthday = self.bri;
        infoVC.gender = self.sex;
        
        [self.navigationController pushViewController:infoVC animated:YES];
    }else{
        
        MyProblemViewController * troubleVC = [MyProblemViewController new];
        
        [self.navigationController pushViewController:troubleVC animated:YES];
    }
    
    SHOW_TAB;
}


- (void)ClickMyselfViewButton:(NSNotification *)noti{
    
    NSString * type = noti.userInfo[@"type"];
    
    HIDDEN_TAB;
    
    if ([type isEqualToString:@"five"]) {
        
        SendSuggestViewController * sendVC = [SendSuggestViewController new];
        
        sendVC.type = @"意见反馈";
        
        [self.navigationController pushViewController:sendVC animated:YES];
    }else if ([type isEqualToString:@"six"]){
        
        SendSuggestViewController * sendVC = [SendSuggestViewController new];
        
        sendVC.type = @"给个好评";
        
        [self.navigationController pushViewController:sendVC animated:YES];
    }else if ([type isEqualToString:@"eight"]){
        
        SettingViewController * settingVC = [SettingViewController new];
        
        [self.navigationController pushViewController:settingVC animated:YES];
    }else if ([type isEqualToString:@"three"]){
        
        MyCollectionViewController * collectVC = [MyCollectionViewController new];
        
        [self.navigationController pushViewController:collectVC animated:YES];
    }else if ([type isEqualToString:@"one"]){
        
        MyTaiViewController * taiVC = [MyTaiViewController new];
        
        taiVC.name = self.name;
        taiVC.url = self.url;
        
        [self.navigationController pushViewController:taiVC animated:YES];
    }else if ([type isEqualToString:@"two"]){
        
        MyReplyViewController * replyVC = [MyReplyViewController new];
        
        [self.navigationController pushViewController:replyVC animated:YES];
    }else if ([type isEqualToString:@"four"]){
        
        MyMsgViewController * msgVC = [MyMsgViewController new];
        
        [self.navigationController pushViewController:msgVC animated:YES];
    }
    
    
    SHOW_TAB;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
